package com.atguigu.gmall.item.service.impl;

import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.product.client.ProductFeignClient;
import com.atguigu.gmall.product.model.*;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author: atguigu
 * @create: 2023-06-10 16:28
 */
@Service
@SuppressWarnings("all")
public class ItemServiceImpl implements ItemService {


    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private ListFeignClient listFeignClient;


    /**
     * 远程调用商品微服务调用七项数据feign接口完成数据汇总
     *
     * @param skuId
     * @return
     */
    @Override
    public Map<String, Object> getItemInfo(Long skuId) {

        //0.验证用户访问商品ID是否存在于布隆过滤器中 TODO:为了开发后续方便,将判断注释掉
        RBloomFilter<Long> bloomFilter = redissonClient.getBloomFilter(RedisConst.SKU_BLOOM_FILTER);
        if (!bloomFilter.contains(skuId)) {
            //布隆过滤器确定有一定误判率;只能用布隆过滤器判断元素可能存在或者一定不存在
            throw new RuntimeException("访问商品不存在!");
        }

        HashMap<String, Object> mapResult = new HashMap<>();

        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //1.根据SkuID查询Sku商品信息-调用SupplyAsync 异步任务有返回结果
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            mapResult.put("skuInfo", skuInfo);
            return skuInfo;
        }, threadPoolExecutor);

        CompletableFuture<Void> categoryViewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            //2.根据商品所属三级分类ID查询分类信息 基于商品信息异步任务,创建新任务(需要获取被依赖任务结果,当前任务不需要返回结果)
            BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
            mapResult.put("categoryView", categoryView);
        }, threadPoolExecutor);

        //3.根据SKuID查询Sku商品价格  - 创建不需要返回结果异步任务对象 runAsnc方法即可
        CompletableFuture<Void> priceCompletableFuture = CompletableFuture.runAsync(() -> {
            BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
            mapResult.put("price", skuPrice);
        }, threadPoolExecutor);

        //4.根据商品SpuID查询海报图片列表
        CompletableFuture<Void> spuPosterCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            List<SpuPoster> spuPosterList = productFeignClient.getSpuPosterBySpuId(skuInfo.getSpuId());
            mapResult.put("spuPosterList", spuPosterList);
        }, threadPoolExecutor);

        //5.根据skuId查询当前SKU商品包含平台属性列表(包含属性值) 创建不需要返回结果异步任务对象 runAsnc方法即可
        CompletableFuture<Void> skuAttrListCompletableFuture = CompletableFuture.runAsync(() -> {
            List<BaseAttrInfo> attrList = productFeignClient.getAttrList(skuId);
            mapResult.put("skuAttrList", attrList);
        }, threadPoolExecutor);

        //6.查询SPU商品所有销售属性,查询指定SKU销售属性选中效果
        CompletableFuture<Void> spuSaleAttrListCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            List<SpuSaleAttr> spuSaleAttrList = productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
            mapResult.put("spuSaleAttrList", spuSaleAttrList);
        }, threadPoolExecutor);


        //7.在一组SPU商品下 切换不同SKU字符串
        CompletableFuture<Void> valuesSkuJsonCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            String skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            mapResult.put("valuesSkuJson", skuValueIdsMap);
        }, threadPoolExecutor);

    //TODO 远程调用搜索服务更新商品热度
    CompletableFuture<Void> hotScoreCompletableFuture = CompletableFuture.runAsync(() -> {
        listFeignClient.incrHotScore(skuId);
    }, threadPoolExecutor);

    //8.汇总所有异步任务执行结果,要求每个任务都必须执行
    CompletableFuture.allOf(
            skuInfoCompletableFuture,
            priceCompletableFuture,
            categoryViewCompletableFuture,
            spuPosterCompletableFuture,
            skuAttrListCompletableFuture,
            spuSaleAttrListCompletableFuture,
            valuesSkuJsonCompletableFuture,
            hotScoreCompletableFuture
    ).join();

        return mapResult;
    }



}
